package com.ckf.login_wx;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.ckf.login_wx.entity.Log;
import com.ckf.login_wx.servic.LogService;
import com.ckf.login_wx.util.HttpContextUtils;

/**
 * @author  作者 jy: 
 * @date 创建时间：2022年1月27日 上午11:07:16 
 * @parameter
 */
@Aspect
@Configuration
public class TestAop {
	@Autowired
	  private LogService LogService;
    /*
     * 定义一个切入点
     */
    // @Pointcut("execution (* findById*(..))")
    @Pointcut("execution(* com.ckf.login_wx.controller.*.*(..))")
    public void excudeService(){}
    /*
     * 通过连接点切入
     */
    @Before("excudeService()")
    public void preHandle(){
    	HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String ipAddress = HttpContextUtils.getIpAddress();
        System.err.println("ip:"+ipAddress);
        System.err.println ("进入切面before");
        String url =request.getRequestURI();
        HttpSession session= request.getSession();
		Object username=session.getAttribute("username");
        Log log =new Log();
		log.setName(username+"");
		log.setIp(ipAddress);
		log.setSrc(url);
		log.setTime(new Date());
        LogService.save(log);
    }

//    @Around("excudeService()")
//    public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint){
//        System.err.println ("切面执行了。。。。");
//        return null;
//    }

}
